<template>
	<view class="width">
		<view class="init_login">
			<view class="init_login_pos">
				<image
					src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-8acfaa58-aeb5-4502-8309-365d4c6cca4a/46f609e3-0d98-4a9a-8c4a-6c3037f00316.png"
					class="init_login_logo"></image>
				<image
					src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-8acfaa58-aeb5-4502-8309-365d4c6cca4a/01c07d26-462d-4fb7-8300-ace950c63793.png"
					class="init_login_meta"></image>
				<view class="init_login_font">XIAOLV KNOWS THE LAW</view>
				<view class="init_message">请登录后继续操作</view>
			</view>

			<view class="init_btn_pos">
				<u-button :custom-style="customStyle" shape="circle" @getphonenumber="loginByWX" :loading="btnLoading"
					open-type="getPhoneNumber">
					<u-icon name="weixin-fill" size="45" style="margin-right: 16rpx;" />微信授权登录
				</u-button>
				<view class="pass_login" @click="jumpLogin('phone')">验证码登录
				</view>
			</view>

			<view class="other-options-container">
				<view class="options-item register" @click="jumpLogin('pass')">
					密码登录
				</view>
				<view class="options-item register" @click="$u.route('/pages/register/register')">
					账号注册
				</view>
				<view class="options-item reset-password" @click="$u.route('/pages/resetPass/resetpass')">
					忘记密码
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				btnLoading: false,
				type: '',
				customStyle: {
					color: '#ffffff',
					background: '#0078fb',
					fontSize: '32rpx',
					padding: '45rpx 0'
				},
			}
		},
		onLoad() {},
		methods: {
			async loginByWX(e) {
				try {
					if (e.detail.code) {
						const GETPHONENUMBERCODE = e.detail.code;
						const {
							openid,unionid
						} = await this.$wx.wxLogin();
						const {
							data
						} = await this.$u.post(this.$restUrl.userLogin, {
							code: GETPHONENUMBERCODE,
							openid,
							unionid
						});
						await this.handlerLogin(data);
					}
				} catch (e) {
					console.log(e);
					if (e.info) {
						this.$showToast(e.info);
					} else {
						this.$showToast('登录失败');
					}

				}
			},
			jumpLogin(type) {
				this.$u.route(`/pages_login/login/login?type=${type}`)
			},
			async handlerLogin(data) {
				try {
					//设置本地已登录用户标识
					this.setCurrentUserLabel(data.customer_id);
					//拿本地数据
					this.getUserLocalStorage();
					//存储token
					this.setTokeSync(data.token);
					//存储环信token
					this.setHXTokeSync(data.hx_token);
					//获取用户信息
					const userInfo = await this.getUserInfo();
					console.log(userInfo)
					//讲用户信息存储在本地做持久化登录
					this.$u.vuex('userInfo', userInfo.data);
					//用户信息判断，如果没有身份就是上一次注册选身份时退出了，如果没有名称头像性别所在地区就是上一次注册时完善信息时退出了，要跳转到对应页面完善信息
					// if (!this.userInfo.customer_type) {
					// 	console.log('没有选身份，跳转到选择身份页面', this.userInfo.customer_type)
					// 	this.$u.route('/pages/selectIdentity/selectIdentity');
					// } else 
					if (!this.userInfo.nickname || !this.userInfo.avatars || !this.userInfo.sex) {
						console.log('没有完善用户信息，跳转到完善用户信息页面', this.userInfo)
						this.$u.route('/pages/authentication/authentication');
					} else {
						console.log('跳转到首页')
						//全局登录状态resolve
						this.$loginResolve();
						//登录环信
						getApp().globalData.webSocketConnect();
						//跳转到首页
						uni.switchTab({
							url: "pages/home/home"
						})
					}
				} catch (err) {
					console.log(err)
					//删除本地用户标识
					this.removeCurrentUserLabel();
					//删除token
					this.removeTokenSync();
					//删除环信token
					this.removeHXTokenSync();
				}
			},
			getUserInfo() {
				return this.$u.post(this.$restUrl.getUserInfo);
			},
		}
	}
</script>

<style scoped lang="scss">
	@import "./loginEntry.scss";
</style>
